﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.Orm.Logic;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.Orm.View;

namespace OpenSoftStore.BusinessLogic.Money
{
    public class MoneyLogic : ABusinessLogic
    {

        public int DeleteAccount(T_Account account)
        {

            if (account.Id == "dc3166e5-75be-4cbf-b02c-30b8e6bc50cb" || account.Id == "df93e6e0-67b2-429c-8623-aa5e60f705f7")
            {
                //FormSysMessage.ShowMessage("系统默认信息，不能删除！");
                string message = string.Format("[{0}]是系统数据，不能删除！", account.Name);
                MessageBox.Show(message);
                return 0;
            }
            if (account.Money > 0)
            {
                string message = string.Format("[{0}]是系统数据，不能删除！", account.Name);
                MessageBox.Show(message);
                return 0;
            }
            return Spa.Delete(account);
        }

        public int DeleteMoney(T_Money money)
        {
            var oldMoney = Spa.Where<T_Money>(m => m.Id == money.Id).FirstOrDefault();
            var account = Spa.Where<T_Account>(m => m.Id == oldMoney.AccountId).FirstOrDefault();
            if (account != null)
            {
                var pay = oldMoney.Type == "收入" ? -oldMoney.Money : oldMoney.Money;
                account.Money = account.Money + pay;
                Spa.UpdateObject(account);
            }
            Spa.DeleteObject(oldMoney);
            int count = Spa.SaveChanges();
            return count;
        }

        public int SaveMoney(T_Money money)
        {
            int count = 0;
            if (string.IsNullOrEmpty(money.Id))
            {
                Spa.AddObject(money);
                var account = Spa.Where<T_Account>(m => m.Id == money.AccountId).FirstOrDefault();
                if (account != null)
                {
                    var pay = money.Type == "收入" ? money.Money : -money.Money;
                    account.Money = account.Money + pay;
                }
                Spa.UpdateObject(account);
            }
            else
            {
                var oldMoney = Spa.Where<T_Money>(m => m.Id == money.Id).FirstOrDefault();
                Spa.UpdateObject(money);
                var account = Spa.Where<T_Account>(m => m.Id == money.AccountId).FirstOrDefault();
                if (account != null)
                {
                    var pay = money.Type == "收入" ? money.Money : -money.Money;
                    account.Money = account.Money + pay;
                    pay = oldMoney.Type == "收入" ? -oldMoney.Money : oldMoney.Money;
                    account.Money = account.Money + pay;
                }
                Spa.UpdateObject(account);
            }
            count =Spa.SaveChanges();
            return count;
        }

        public int AddMoeny(V_Money money)
        {
            if (money == null) return 0;

            var lMoney = new L_Money
            {
                TreeId = "dc3166e5-75be-4cbf-b02c-30b8e6bc50cb",
                TreeName = "",
                Type = "收入",
                ObjectId = money.ObjectId,
                BankPayMoney = money.PayType == "银行转账" ? 1 : 0,
                CashPayMoney = money.PayType == "现金" ? 1 : 0,
                BankAccountId = money.AccountId,
                AdminId = money.AdminId,
                ShopId = money.ShopId
            };
            return AddMoeny(lMoney);
           
        }

        public List<V_Money> GetMoneyView(string strWhere)
        {
            string sql = "" +
                 "select " +
                 "  T_Money.*,T_Tree.Name as TreeName,T_Account.Name as AccountName " +
                 "from " +
                 "  (T_Money " +
                 "  left join T_Tree on T_Money.TreeId=T_Tree.Id)" +
                 "  left join T_Account on T_Money.AccountId=T_Account.Id ";
            if (!string.IsNullOrEmpty(strWhere)) sql += strWhere;
            return Spa.SelectWhere<V_Money>(sql).ToList();
        }

        public DataTable GetMoneyDataTable(string strWhere)
        {
            string sql = "" +
                 "select " +
                 "  T_Money.*,T_Tree.Name as TreeName,T_Account.Name as AccountName " +
                 "from " +
                 "  (T_Money " +
                 "  left join T_Tree on T_Money.TreeId=T_Tree.Id)" +
                 "  left join T_Account on T_Money.AccountId=T_Account.Id ";
            if (!string.IsNullOrEmpty(strWhere)) sql += strWhere;
            var data = Spa.RunSqLcmdDataSet(sql);
            if(data==null) return new DataTable();
            if(data.Tables.Count==0) return new DataTable();
            return data.Tables[0];
        }

        public int AddMoeny(L_Money lMoney)
        {
            int counts = 0;
            if (lMoney.Type == "支出")
            {
                lMoney.CashPayMoney = -lMoney.CashPayMoney;
                lMoney.BankPayMoney = -lMoney.BankPayMoney;
            }
            if (lMoney.CashPayMoney != 0)
            {
                var money = new T_Money();
                Spa.CopyPropertiesFrom(money, lMoney);
                money.Money = lMoney.CashPayMoney;
                money.AccountId = "dc3166e5-75be-4cbf-b02c-30b8e6bc50cb";
                money.Remark = GetRemark(lMoney, "现金");
                Spa.AddObject(money);
                var account=Spa.Where<T_Account>(m => m.Id == money.AccountId).FirstOrDefault();
                if (account == null) account = Spa.Where<T_Account>(m => m.Name == "现金").FirstOrDefault();
                if (account != null)
                {
                    account.Money = account.Money + lMoney.CashPayMoney;
                    Spa.UpdateObject(account);
                }

            }

            if (lMoney.BankPayMoney != 0)
            {
                var money = new T_Money();
                Spa.CopyPropertiesFrom(money, lMoney);
                money.AccountId = lMoney.BankAccountId;
                money.Money = lMoney.BankPayMoney;
                money.Remark = GetRemark(lMoney, "银行转账");
                Spa.AddObject(money);
                var account = Spa.Where<T_Account>(m => m.Id == money.AccountId).FirstOrDefault();
                if (account != null)
                {
                    account.Money = account.Money + lMoney.BankPayMoney;
                    Spa.UpdateObject(account);
                }
            }
            int count = Spa.SaveChanges();
            return count;
        }

        private string GetRemark(L_Money lMoney, string payType = "")
        {
            string remark = "";
            string money = "";
            if (payType == "现金")
                money = lMoney.CashPayMoney.ToString("0.00");
            else if (payType == "银行转账")
                money = lMoney.BankPayMoney.ToString("0.00");
            if (!string.IsNullOrEmpty(lMoney.CardId)) remark += string.Concat(new string[] { "卡号为：", lMoney.CardId});
            if (!string.IsNullOrEmpty(lMoney.Name)) remark += string.Concat(new string[] { "[", lMoney.CardId,"]" });
            remark += string.Concat(new string[] { "的", lMoney.TreeName, payType, money, "元" });
            return remark;
        }

        //public List<T_Money> GetMoneyReport(string strWhere)
        //{
        //    List<T_Money> dt = base.SqlWhere<T_Money>(strWhere).ToList();
        //    return dt;
        //}

        public List<V_Money> GetMoneyReport(string strWhere)
        {
            string sql = "" +
             "select " +
             "  T_Money.*,T_Tree.Name as TreeName " +
             "from " +
             "  T_Money " +
             "  inner join T_Tree on T_Money.TreeId=T_Tree.Id ";
            if (!string.IsNullOrEmpty(strWhere)) sql += strWhere;
            List<V_Money> dt = Spa.SelectWhere<V_Money>(sql).ToList();
            return dt;
        }

        public DataTable GetMoneyGroupReport(string strWhere)
        {
            string sql = "select * from (" +
                         "select " +
                         "   Max(T_Tree.Id) as TreeId,Max(T_Tree.Name) as TreeName,Max(T_Money.Type) as MoneyType,Sum(T_Money.Money) as SumMoney " +
                         "from " +
                         "  T_Money " +
                         "  inner join T_Tree on T_Money.TreeId=T_Tree.Id ";
            if (!string.IsNullOrEmpty(strWhere)) sql += strWhere;
            sql += "group by T_Tree.Name )";

            if (Spa.RunSqLcmdDataSet(sql).Tables.Count > 0)
                return Spa.RunSqLcmdDataSet(sql).Tables[0];
            else
                return null;
        }
        public DataTable GetMoneySumNamber(string strWhere)
        {
            string sql = "select Type,ObjectId2, Sum(Money) as Moneys  from T_Money where 1=1 " + strWhere + " group by Type,ObjectId2";
            if (Spa.RunSqLcmdDataSet(sql).Tables.Count > 0)
                return Spa.RunSqLcmdDataSet(sql).Tables[0];
            else
                return null;
        }

        public DataTable GetAccountDataTable(string strWhere)
        {
            string sql = "select * from T_Account " + strWhere;
            var data = Spa.RunSqLcmdDataSet(sql);
            if (data==null) return new DataTable();
            if(data.Tables.Count==0) return new DataTable();
            return data.Tables[0];
        }
    }
}
